package com.fangngng.pushall.service.impl;

import com.fangngng.pushall.constant.ErrorCode;
import com.fangngng.pushall.dto.AddTagVO;
import com.fangngng.pushall.dto.Result;
import com.fangngng.pushall.dto.TagQuery;
import com.fangngng.pushall.dto.TopicQuery;
import com.fangngng.pushall.entity.Tag;
import com.fangngng.pushall.entity.Topic;
import com.fangngng.pushall.entity.TopicTag;
import com.fangngng.pushall.entity.User;
import com.fangngng.pushall.entity.UserSubscribeTag;
import com.fangngng.pushall.exception.BusinessException;
import com.fangngng.pushall.mapper.TagMapper;
import com.fangngng.pushall.mapper.TopicMapper;
import com.fangngng.pushall.mapper.TopicTagMapper;
import com.fangngng.pushall.mapper.UserSubscribeTagMapper;
import com.fangngng.pushall.service.TagService;
import com.fangngng.pushall.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author fei.fang
 * @date 2021/4/9
 **/
@Slf4j
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TopicTagMapper topicTagMapper;

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private UserSubscribeTagMapper userSubscribeTagMapper;

    @Autowired
    private UserService userService;

    @Override
    public Result<String> subscribeTag(Integer tagId, String userEmail) {
        User userDb = userService.findByEmail(userEmail);
        if (userDb == null) {
            log.warn("用户:{}不存在", userEmail);
            throw new BusinessException(ErrorCode.ERROR_LOGIN_USER_NOT_EXIST);
        }

        Tag record = new Tag();
        record.setId(tagId);
        Tag tag = tagMapper.selectOne(record);
        if (tag == null) {
            log.warn("订阅的标签不存在");
            throw new BusinessException(ErrorCode.ERROR_TAG_NOT_EXIST);
        }

        UserSubscribeTag ust = new UserSubscribeTag();
        ust.setUserId(userDb.getId());
        ust.setTagId(tagId);
        List<UserSubscribeTag> select = userSubscribeTagMapper.select(ust);
        if (!select.isEmpty()) {
            log.warn("已经订阅过主题了");
            return Result.ok();
        }

        ust.setCreateTime(new Date());
        userSubscribeTagMapper.insert(ust);

        // todo 并发情况
        tag.setSubCount(tag.getSubCount()+1);
        tagMapper.updateByPrimaryKeySelective(tag);

        return Result.ok();
    }

    @Override
    public Result<Tag> addTag(AddTagVO addTagVO, String userEmail) {
        User userDb = userService.findByEmail(userEmail);
        if (userDb == null) {
            log.warn("用户:{}不存在", userEmail);
            throw new BusinessException(ErrorCode.ERROR_LOGIN_USER_NOT_EXIST);
        }

        Tag tag = new Tag();
        tag.setName(addTagVO.getName());
        tag.setStatus(Tag.LIFECYCLE_ENABLE);
        List<Tag> tags = tagMapper.select(tag);
        if (!tags.isEmpty()) {
            log.warn("标签已存在");
            return Result.ok(tags.get(0));
        }

        tag.setStatus(Tag.LIFECYCLE_DISABLE);
        List<Tag> select = tagMapper.select(tag);
        if (!select.isEmpty()) {
            Tag tagDb = select.get(0);
            tagDb.setStatus(Tag.LIFECYCLE_ENABLE);
            tagMapper.updateByPrimaryKeySelective(tagDb);
            return Result.ok(tagDb);
        }

        tag.setCreateTime(new Date());
        tag.setCreateBy(userDb.getId());
        tagMapper.insert(tag);

        return Result.ok(tag);
    }

    @Override
    public List<Topic> getTopicByTag(AddTagVO addTagVO) {

        Tag tag = new Tag();
        tag.setName(addTagVO.getName());
        tag.setStatus(Tag.LIFECYCLE_ENABLE);
        List<Tag> tags = tagMapper.select(tag);
        if (tags.isEmpty()) {
            log.warn("标签不存在存在");
            throw new BusinessException(ErrorCode.ERROR_TAG_NOT_EXIST);
        }

        Tag tagDb = tags.get(0);
        List<Topic> topics = topicMapper.findTopicByTag(tagDb.getId());
        return topics;
    }

    @Override
    public List<Tag> getHottest10Tag() {
        return tagMapper.getHottest10Tag();
    }

    @Override
    public List<Tag> getNewest10Tag() {
        return tagMapper.getNewest10Tag();
    }

    @Override
    public PageInfo<Tag> getSubTagByPage(int pageNumber, int pageSize, String sort, TagQuery topicQuery) {
        PageHelper.startPage(pageNumber, pageSize);
        PageHelper.orderBy(sort);
        List<Tag> topicList = tagMapper.findByCondition(topicQuery);
        PageInfo pageInfo = new PageInfo<>(topicList, pageSize);
        return pageInfo;
    }
}
